/*创建ajax对象*/
function add_ajax()
{
	var xmlhttp=null;
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			xmlhttp=null;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != "undefined")
	{
		xmlhttp=new XMLHttpRequest();
	}
    return xmlhttp;
}

function url_ajax(url)
{
	var randname = "rand"+Math.floor(Math.random()*10000);
	var randnum = Math.floor(Math.random()*10000);
	if(url.indexOf("?") != -1)
	{
		u_length = url.length;
		if(url.substr((u_length-1)-u_length) != "&")
		{
			url += "&"+randname+"="+randnum;
		}
		else
		{
			url += randname+"="+randnum;
		}
	}
	else
	{
		url += "?"+randname+"="+randnum;
	}
	return url;
}

/*根据url来处理ajax的信息*/
function get_ajax(url,ajax_func,ext)
{
	if(ajax_func && ajax_func != "undefined")
	{
		get_ajax_asyn(url,ajax_func,ext);
		return true;
	}
	else
	{
		return get_ajax_syn(url);
	}
}

/*异步处理*/
function get_ajax_asyn(url,func,ext)
{
	var xmlhttp = add_ajax();
	url = url_ajax(url);
	xmlhttp.onreadystatechange = function(){func(xmlhttp,ext)}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	return xmlhttp;
}

function ajax_result(xmlhttp)
{
	if(xmlhttp.readyState == 4)
	{
		var result = xmlhttp.responseText;
		if(!result) result = "false";
		return result;
	}
	else
	{
		return false;
	}
}

/*同步处理*/
function get_ajax_syn(url)
{
	var xmlhttp = add_ajax();
	url = url_ajax(url);
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	if(xmlhttp.readyState == 4)
	{
		var result = xmlhttp.responseText;
		if(!result) result = "false";
		return result;
	}
	else
	{
		alert("数据加载中...");
		return true;
	}
}


/* 前台通过JS获取数据来处理PHP信息 */
function func_php(module,func,id)
{
	var url = get_url("js","act="+module);
	get_ajax(url,func,id);
}

/** 以下是常用的核心JS信息 **/
function js_usercp(xmlhttp,id)
{
	var msg = ajax_result(xmlhttp);
	if(msg && msg != "false")
	{
		getid(id).innerHTML = msg;
	}
	return true;
}

//退出操作
function logout()
{
	var q = confirm("确定要退出吗？");
	if(q != "0")
	{
		var url = get_url("js","act=logout");
		get_ajax(url);
		direct(window.location.href);
	}
}

//加载点评中心
function js_reply(xmlhttp,id)
{
	var msg = ajax_result(xmlhttp);
	if(msg && msg != "false")
	{
		getid(id).innerHTML = msg;
	}
	return true;
}

//显示点评模块
function js_show_digg(xmlhttp,id)
{
	var msg = ajax_result(xmlhttp);
	if(getid(id) && msg != "false" && msg != "clicked" && msg)
	{
		getid(id).style.display = "";
		getid(id).innerHTML = msg;
	}
	else if(msg == "clicked")
	{
		var val = "You have clicked on, please do not repeat it.";
		if(getid("clicked_val") && getid("clicked_val").value)
		{
			var val = getid("clicked_val").value;
		}
		alert(val);
		return false;
	}
	return true;
}

function over_tr(v)
{
	v.className = "tr_over";
}
function out_tr(v)
{
	v.className = "tr_out";
}

//加入购物车
function addcart(id)
{
	var url = base_file + "?"+base_ctrl+"=cart&"+base_func+"=ajax_add&id="+id;
	var msg = get_ajax(url);
	if(msg == "ok")
	{
		window.location.href = base_file + "?"+base_ctrl+"=cart";
	}
	else
	{
		if(!msg) msg = "error!";
		alert(msg);
		return false;
	}
}

//语言选择
function lang_select(val)
{
	var url = base_file + "?langid="+val;
	window.location.href = url;
}